<?php

function connectToDB() 
{
	global $link, $dbhost, $dbuser, $dbpass, $dbname;
	($link = mysql_pconnect("$dbhost", "$dbuser", "$dbpass")) || die("Couldn't connect to MySQL");
	mysql_select_db("$dbname", $link) || die("Couldn't open db: $dbname. Error if any was: ".mysql_error() );
} 

function changePwd($login, $password) 
{
        //global $link;
	global $tabla_data, $tabla_tracking, $tabla_user, $link;
        $query="UPDATE ".$tabla_user." set password=SHA1('".$password."') where user_id='".$login."'";
        $result=mysql_query($query, $link) or die("Died inserting login info into db.  Error returned if any: ".mysql_error());
        return true;
} 

function insertuser($login, $password)
{
        //global $link;
        global $tabla_data, $tabla_tracking, $tabla_user, $link;
        $query="INSERT INTO ".$tabla_user." (user_id,password) VALUES ('".$login."',SHA1('".$password."'))"; 
        $result=mysql_query($query, $link) or die("Died inserting login info into db.  Error returned if any: ".mysql_error());
        return true;
}



function displayErrors($messages) 
{
	print("<b>There were problems with the previous action.  Following is a list of the error messages generated:</b>\n<ul>\n");
	foreach($messages as $msg)
	{
		print("<li>$msg</li>\n");
	}
	print("</ul>\n");
}


function checkLoggedIn($status)
{
	switch($status)
	{
		case "yes":
			if(!isset($_SESSION["loggedIn"]))
			{
				header("Location: login.php");
				exit;
			}
			break;
		case "no":
			if(isset($_SESSION["loggedIn"]) && $_SESSION["loggedIn"] === true )
			{
				header("Location: members.php");
			}
			break;
	}
	return true;
} 

function checkPass($login, $password)
{
	global $tabla_data, $tabla_tracking, $tabla_user, $link;	
	$query="SELECT user_id, password FROM ".$tabla_user." WHERE user_id='".$login."' and password=SHA1('".$password."')";
	$result=mysql_query($query, $link)
		or die("checkPass fatal error: ".mysql_error());

	if(mysql_num_rows($result)==1) {
		$row=mysql_fetch_array($result);
		return $row;
	}
	return false;
}

function checknewuser($login)
{
        global $tabla_data, $tabla_tracking, $tabla_user, $link;
        $query="SELECT user_id FROM ".$tabla_user." WHERE user_id='".$login."'";
        $result=mysql_query($query, $link)
                or die("checkPass fatal error: ".mysql_error());

        if(mysql_num_rows($result)>0) {
                $row=mysql_fetch_array($result);
                return 1;
        }
        return 0;
}

function cleanMemberSession($login, $password) 
{
	session_start();
	$_SESSION["login"]=$login;
	$_SESSION["password"]=$password;
	$_SESSION["loggedIn"]=true;
} 

function flushMemberSession() 
{
	unset($_SESSION["login"]);
	unset($_SESSION["password"]);
	unset($_SESSION["loggedIn"]);
	session_destroy();
	return true;
} 


function doCSS() 
{
	?>
<style type="text/css">
body{font-family: Arial, Helvetica; font-size: 10pt}
h1{font-size: 12pt}
</style>
	<?php
} 

# function validates HTML form field data passed to it:
function field_validator($field_descr, $field_data, $field_type, $min_length="", $max_length="", $field_required=1)
{
	global $messages;
	if(!$field_data && !$field_required){ return; }
	$field_ok=false;
	$email_regexp="^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|";
	$email_regexp.="(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
	$data_types=array(
		"email"=>$email_regexp,
		"digit"=>"/^[0-9]$/",
		"number"=>"/^[0-9]+$/",
		"alpha"=>"/^[a-zA-Z]+$/",
		"alpha_space"=>"/^[a-zA-Z ]+$/",
		"alphanumeric"=>"/^[a-zA-Z0-9]+$/",
		"alphanumeric_space"=>"/^[a-zA-Z0-9 ]+$/",
		"string"=>"//"
	);

	# check for required fields
	if ($field_required && empty($field_data)) {
		$messages[] = "$field_descr is a required field.";
		return;
	}

	# if field type is a string, no need to check regexp:
	if ($field_type == "string") {
		$field_ok = true;
	} else {
		# Check the field data against the regexp pattern:
		//$field_ok = ereg($data_types[$field_type], $field_data);
		$field_ok = preg_match($data_types[$field_type], $field_data);
	}

	# if field data is bad, add message:
	if (!$field_ok) {
		$messages[] = "Please enter a valid $field_descr.";
		return;
	}

	# field data min length checking:
	if ($field_ok && ($min_length > 0)) {
		if (strlen($field_data) < $min_length) {
			$messages[] = "$field_descr is invalid, it should be at least $min_length character(s).";
			return;
		}
	}

	# field data max length checking:
	if ($field_ok && ($max_length > 0)) {
		if (strlen($field_data) > $max_length) {
			$messages[] = "$field_descr is invalid, it should be less than $max_length characters.";
			return;
		}
	}
}
?>
